export default {
  namespaced: true,
  state: {
    tagsList: []
  },
  mutations: {
    setTags(state, route) {
      const isExist = state.tagsList.some(item => {
        return item.path === route.fullPath
      })
      !isExist && state.tagsList.push({
        title: route.meta.title,
        path: route.fullPath
      })
    },
    clearTags(state) {
      state.tagsList = []
    },
    closeAllTags(state, vm) {
      setTimeout(() => {
        state.tagsList = []
        state.tagsList.push({
          title: '首页',
          path: '/index'
        })
        vm.$router.push('/index')
      }, 10)
    },
    closeTags(state, vm) {
      let index = state.tagsList.findIndex(item => item.path === vm.path)
      if (state.tagsList.length === 1 && state.tagsList[0].path === '/index') {
        return false
      }
      const delItem = state.tagsList.splice(index, 1)[0]
      const item = state.tagsList[index] ? state.tagsList[index] : state.tagsList[index - 1]
      if (item) {
        delItem.path === vm.$route.fullPath && vm.$router.push(item.path)
      } else {
        vm.$router.push('/')
      }
    },
    closeOtherTags(state, vm) {
      setTimeout(() => {
        state.tagsList = []
        state.tagsList.push(vm.selectedTag)
        vm.$router.push(vm.path)
      }, 10)
    },
    closeCurrent(state, vm) {
      let index = state.tagsList.findIndex(item => item.path === vm.path)
      state.tagsList.splice(index, 1)
      if (vm.toPath !== '' && vm.toPath !== undefined && vm.toPath !== null && vm.toPath) {
        let isList = state.tagsList.findIndex(item => item.path === vm.toPath)
        if (isList > 0) {
          state.tagsList.splice(isList, 1)
        }
        setTimeout(() => {
          vm.$router.push(vm.toPath)
        }, 10)
      }
    }
  },
  actions: {}
}
